package com.example.controller;

import com.example.model.entity.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/page")
public class PageController {
    private final String prefix = "http://localhost:63342/StudentManageSystem-Thymeleaf/templates";
    
    @GetMapping({"/", "/login"})
    public String index() {
        return "login";
    }
    
    @GetMapping("/teacher")
    public String teacher(HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return "redirect:/page/login";
        }
        return "redirect:"+prefix+"/teacher.html";
    }

    @GetMapping("/student")
    public String student(HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        if (user == null || user.getRole() != 2) {
            return "redirect:/page/login";
        }
        return "redirect:"+prefix+"/student.html";
    }

    @GetMapping("/admin")
    public String admin(HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        if (user == null || user.getRole() != 0) {
            return "redirect:/page/login";
        }
        return "redirect:"+prefix+"/admin.html";
    }
    
    @GetMapping("/course/list")
    public String courseList(HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return "redirect:/page/login";
        }
        return "redirect:"+prefix+"/course/list.html";
    }
    
    @GetMapping("/grade/list")
    public String gradeList(HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return "redirect:/page/login";
        }
        return "redirect:"+prefix+"/grade/list.html";
    }
    
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:"+prefix+"/login.html";
    }
} 